package com.hao.shirojwt.util.response;


import com.hao.shirojwt.util.ResultEnum;
import lombok.*;


@Setter
@Getter
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BaseResponse<T> {
    private String code;
    private String msg;
    private T data;

    public BaseResponse(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public BaseResponse(String code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public BaseResponse(T data) {
        this.code = ResultEnum.SUCCESS.getCode();
        this.msg = ResultEnum.SUCCESS.getMsg();
        this.data = data;
    }

    public BaseResponse(Exception e, T data) {
        this.code = ResultEnum.ERR.getCode();
        this.msg = e.getMessage();
        this.data = data;
    }

    public BaseResponse(ResultEnum resultEnum, Exception e, T data) {
        this.code = resultEnum.getCode();
        this.msg = e.getLocalizedMessage();
        this.data = data;
    }

    public BaseResponse(String code, Exception e) {
        this.code = code;
        this.msg = e.getLocalizedMessage();
    }

    public static <T> BaseResponse<T> ok() {
        return new BaseResponse<>(ResultEnum.SUCCESS);
    }
    public static <T> BaseResponse<T> ok(T data) {
        return new BaseResponse<>(data);
    }

    public static <T> BaseResponse<T> error(String msg) {
        return new BaseResponse<T>(ResultEnum.ERR.getCode(), msg);
    }
}